using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using ENTITIES;

namespace DAL
{
   public class SanPhamDAL
    {
       private SqlConnection cn = null;
        public SanPhamDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
       public void Insert (TBSanPham sp)
        {
            SqlCommand cm = new SqlCommand("SanPham_insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaDN",sp.madn);
            cm.Parameters.AddWithValue("@TenSP", sp.tensp);
            cm.Parameters.AddWithValue("@ChietKhau", sp.chitietSP);
            cm.Parameters.AddWithValue("@ChiTietSL", sp.chitietSP);
            cm.Parameters.AddWithValue("@SLPhieu", sp.slphieu);
            cm.Parameters.AddWithValue("@GiaGoc", sp.giagoc);
            cm.Parameters.AddWithValue("@GiaBan", sp.giaban);
            cm.Parameters.AddWithValue("@ThoiGianBD", sp.thoigianbd);
            cm.Parameters.AddWithValue("@ThoiGianKT", sp.thoigiankt);
            cm.Parameters.AddWithValue("@DieuKien", sp.dieukien);
            cm.Parameters.AddWithValue("@TrangThai", sp.trangthai);
            cm.Parameters.AddWithValue("@MaLoai", sp.maloai);
            cm.Parameters.AddWithValue("@SLPhieuBan", sp.slphieuban);
            cm.Parameters.AddWithValue("@Hinh", sp.hinh);
            cm.Parameters.AddWithValue("@DiemDen", sp.diemden);

            cm.ExecuteNonQuery();
        }
        public void Update(TBSanPham sp)
        {
            SqlCommand cm = new SqlCommand("SanPham_update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaSP", sp.masp);
            cm.Parameters.AddWithValue("@MaDN", sp.madn);
            cm.Parameters.AddWithValue("@TenSP", sp.tensp);
            cm.Parameters.AddWithValue("@ChietKhau", sp.chitietSP);
            cm.Parameters.AddWithValue("@ChiTietSL", sp.chitietSP);
            cm.Parameters.AddWithValue("@SLPhieu", sp.slphieu);
            cm.Parameters.AddWithValue("@GiaGoc", sp.giagoc);
            cm.Parameters.AddWithValue("@GiaBan", sp.giaban);
            cm.Parameters.AddWithValue("@ThoiGianBD", sp.thoigianbd);
            cm.Parameters.AddWithValue("@ThoiGianKT", sp.thoigiankt);
            cm.Parameters.AddWithValue("@DieuKien", sp.dieukien);
            cm.Parameters.AddWithValue("@TrangThai", sp.trangthai);
            cm.Parameters.AddWithValue("@MaLoai", sp.maloai);
            cm.Parameters.AddWithValue("@SLPhieuBan", sp.slphieuban);
            cm.Parameters.AddWithValue("@Hinh", sp.hinh);
            cm.Parameters.AddWithValue("@DiemDen", sp.diemden);
            cm.ExecuteNonQuery();
        }
    
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("SanPham_delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaSP", iD);
            cm.ExecuteNonQuery();
        }

        public tbSanPhamCollection Get(string where, int pageIndex, int pageSize,
            string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("SanPham_get", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            tbSanPhamCollection sanphamCollection = new tbSanPhamCollection();

            while (dr.Read())
            {
                TBSanPham sanpham = new TBSanPham();
                sanpham.masp = int.Parse(dr.GetValue(0).ToString());
                sanpham.madn = int.Parse(dr.GetValue(1).ToString());
                sanpham.tensp = dr.GetValue(2).ToString();
                sanpham.chietkhau = dr.GetValue(3).ToString();
                sanpham.chitietSP = dr.GetValue(4).ToString();
                sanpham.slphieu = int.Parse(dr.GetValue(5).ToString());
                sanpham.giagoc = int.Parse(dr.GetValue(6).ToString());
                sanpham.giaban = int.Parse(dr.GetValue(7).ToString());
                sanpham.thoigianbd = dr.GetValue(8).ToString();
                sanpham.thoigiankt = dr.GetValue(9).ToString();
                sanpham.dieukien = dr.GetValue(10).ToString();
                sanpham.trangthai = int.Parse(dr.GetValue(11).ToString());
                sanpham.maloai = int.Parse(dr.GetValue(12).ToString());
                sanpham.slphieuban = int.Parse(dr.GetValue(13).ToString());
                sanpham.hinh = int.Parse(dr.GetValue(14).ToString());
                sanpham.diemden = int.Parse(dr.GetValue(15).ToString());

                sanphamCollection.Add(sanpham);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return sanphamCollection;
        }
    }
    }

